

package com.legstar.test.coxb.tcobwvb.bind;

import com.legstar.coxb.ICobolBinding;
import com.legstar.coxb.common.CChoiceBinding;
import com.legstar.coxb.ICobolStringBinding;
import com.legstar.coxb.CobolBindingFactory;
import com.legstar.coxb.ICobolBindingFactory;
import com.legstar.coxb.ICobolComplexBinding;
import com.legstar.test.coxb.tcobwvb.Filler12;
import com.legstar.coxb.host.HostException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.legstar.test.coxb.tcobwvb.Transaction;

/**
 * LegStar Binding for Choice element:
 *   TransactionDate
 *   Filler12
 * 
 * Represents a choice between 2 or more elements. A choice maps to a cobol
 * REDEFINES clause exposed as an xs:choice in the corresponding XML schema
 * 
 * This class was generated by LegStar Binding generator.
 */
public class TransactionDateChoiceBinding 
             extends CChoiceBinding {

    /** Alternative bound bound to value object property TransactionDate(String). */
    public ICobolStringBinding _transactionDate;
    /** Alternative bound bound to value object property Filler12(Filler12). */
    public ICobolComplexBinding _filler12;

    /** Logger. */
    private final Log _log = LogFactory.getLog(getClass());

    /** Binding factory. */
    private static final ICobolBindingFactory BF
        = CobolBindingFactory.getBindingFactory();
    
    /**
     * Constructor for a Choice element.
     * 
     * @param bindingName the identifier for this binding
     * @param parentBinding a reference to the parent binding
     */
    public TransactionDateChoiceBinding(
            final String bindingName,
            final ICobolComplexBinding parentBinding) {
        
        super(bindingName, null, parentBinding);
        initAlternatives();
    }

    /** Creates a binding property for each alternative. */
    private void initAlternatives() {
        if (_log.isDebugEnabled()) {
            _log.debug("Initializing started");
        }
        /* Create binding alternatives instances */

        _transactionDate = BF.createStringBinding("TransactionDate",
               "TransactionDate", String.class, getParentBinding());
        _transactionDate.setCobolName("TRANSACTION-DATE");
        _transactionDate.setByteLength(8);
        _filler12 = new Filler12Binding("Filler12",
               "Filler12", getParentBinding(), null);
        _filler12.setCobolName("FILLER");
        _filler12.setByteLength(8);
        _filler12.setRedefines("TRANSACTION-DATE");

        /* Add alternatives to alternatives list */
        addAlternative(_transactionDate);
        addAlternative(_filler12);
 
        if (_log.isDebugEnabled()) {
            _log.debug("Initializing successful");
        }
    }
 
    /** {@inheritDoc} */
    public void setAlternativesValues() throws HostException {
        Object value;
        value = getTransaction().getTransactionDate();
        if (value != null) {
            if (_log.isDebugEnabled()) {
                _log.debug("Getting value from Value object property "
                        + "TransactionDate"
                        + " value=" + value);
            }
            _transactionDate.setObjectValue(value);
        }
        value = getTransaction().getFiller12();
        if (value != null) {
            if (_log.isDebugEnabled()) {
                _log.debug("Getting value from Value object property "
                        + "Filler12"
                        + " value=" + value);
            }
            _filler12.setObjectValue(value);
        }

    }
    /** {@inheritDoc} */
    public void setJaxbPropertyValue(
            final int index) throws HostException {
        /* Set the Value object property value from binding object */
        ICobolBinding alt = getAlternativesList().get(index);

      /* Alternatives that are not bound to a value object property are ignored.
       * This includes Choices and dynamically generated counters
       * for instance.  */
        if (!alt.isBound()) {
            return;
        }

        Object bindingValue = alt.getObjectValue(alt.getJaxbType());
        if (_log.isDebugEnabled()) {
            _log.debug("Setting value of Value object property "
                    + alt.getJaxbName()
                    + " value=" + bindingValue);
        }
        switch (index) {
        case 0:
            getTransaction().setTransactionDate(
                (String) bindingValue);
            break;
        case 1:
            getTransaction().setFiller12(
                (Filler12) bindingValue);
            break;
        default:
            break;
        }
    }
    
    /** {@inheritDoc} */
    public Object getObjectValue(
            final Class < ? >  type) throws HostException {
        throw new HostException("Attempt to get value from choice binding "
                + getCobolName());
    }

    /** {@inheritDoc} */
    public void setObjectValue(final Object value) throws HostException {
        throw new HostException("Attempt to set value for choice binding "
                + getCobolName());
    }
    
    /** {@inheritDoc} */
    public boolean isSet() {
        /* A Choice is considered set if at least one of its alternatives
         * is set. */
        for (ICobolBinding alt : getAlternativesList()) {
            if (alt.isSet()) {
                return true;
            }
        }
        return false;
    }

    /** {@inheritDoc} */
    public Object getParentValueObject() throws HostException {
        return ((TransactionBinding) getParentBinding()).getTransaction();
    }

    /**
     * @return the Value object bound to the parent of this choice
     * @throws HostException if parent bound Value object cannot be retrieved
     */
    public Transaction getTransaction() throws HostException {
        return (Transaction) getParentValueObject();
    }
    
}

